package com.etrans.web.gps.business.log.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.log.domain.LoginRecordVO;
import com.etrans.web.gps.common.MGTemplete;
import com.etrans.web.gps.common.page.PageVo;


/**
 * 登录日志（MangoDB）持久化类
 * @author zhangw
 *
 */
@Service
public class LoginRecordService extends BaseService{
	
	
	/**
     * 新增日志
     */
    public void insertLoginRecord(LoginRecordVO log){
        MGTemplete.insertOne("LogRecord", "LoginRecord",log);
    }
    
    /**
     * 根据条件查询登录日志
     * @return 日志list
     */
    public List<LoginRecordVO> queryLoginRecordList(HashMap<String,Object> paramMap){
    	//查询条件
		HashMap<String,Object> whereMap = new HashMap<String, Object>();
		HashMap<String,Object> whereMap_Time = new HashMap<String, Object>();
		HashMap<String,Object> whereMap_User = new HashMap<String, Object>();
		HashMap<String,Object> whereMap_Unit = new HashMap<String, Object>();
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String userName = (String) paramMap.get("userName");
			String workUnitName = (String) paramMap.get("workUnitName");
			String start_Time = (String) paramMap.get("startTime");
			String end_Time = (String) paramMap.get("endTime");
			Date startTime = format.parse(start_Time);
			Date endTime = format.parse(end_Time);

			if ( null != userName && !"".equals(userName)) {
				whereMap_User.put("$regex", userName);
				whereMap.put("userName", whereMap_User);
			}
			if ( null != workUnitName && !"".equals(workUnitName)) {
				whereMap_Unit.put("$regex", workUnitName);
				whereMap.put("workUnitName", whereMap_Unit);
			}
			if (null != start_Time && !"".equals(start_Time)) {
				whereMap_Time.put("$gte", startTime);	//大于
			}
			if (null != end_Time && !"".equals(end_Time)) {
				whereMap_Time.put("$lte", endTime);		//小于
			}
			whereMap.put("loginTime", whereMap_Time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		//加入排序条件sortMap
		HashMap<String,Object> sortMap = new HashMap<String,Object>();
		sortMap.put("login_time", 1);

		return null;
    }
    
	/***
	 * 根据条件分页查询
	 */
	public PageVo getLoginRecordList(Map<String, Object> param) {
		PageVo pageVo = new PageVo();
		
    	//处理查询条件
		HashMap<String,Object> whereMap = new HashMap<String, Object>();
		HashMap<String,Object> whereMap_Time = new HashMap<String, Object>();
		HashMap<String,Object> whereMap_User = new HashMap<String, Object>();
		HashMap<String,Object> whereMap_Unit = new HashMap<String, Object>();
		HashMap<String,Object> sortMap = null;
		int pageSize = 10;int page = 1;
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			pageSize = Integer.parseInt(String.valueOf(param.get("pageSize")));
			page = Integer.parseInt(String.valueOf(param.get("page")));
			String userName = (String) param.get("userName");
			String workUnitName = (String) param.get("workUnitName");
			String start_Time = (String) param.get("startTime");
			String end_Time = (String) param.get("endTime");
			Date startTime = format.parse(start_Time);
			Date endTime = format.parse(end_Time);

			if ( null != userName && !"".equals(userName)) {
				whereMap_User.put("$regex", userName);
				whereMap.put("userName", whereMap_User);
			}
			if ( null != workUnitName && !"".equals(workUnitName)) {
				whereMap_Unit.put("$regex", workUnitName);
				whereMap.put("workUnitName", whereMap_Unit);
			}
			if (null != start_Time && !"".equals(start_Time)) {
				whereMap_Time.put("$gte", startTime);	//大于
			}
			if (null != end_Time && !"".equals(end_Time)) {
				whereMap_Time.put("$lte", endTime);		//小于
			}
			whereMap.put("loginTime", whereMap_Time);
			//加入排序条件sortMap
			sortMap = new HashMap<String,Object>();
			sortMap.put("loginTime", -1);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Long total = MGTemplete.findCount("LogRecord", "LoginRecord",whereMap);
		List<LoginRecordVO> list = MGTemplete.find("LogRecord", "LoginRecord",whereMap,sortMap,page,pageSize,LoginRecordVO.class);
		pageVo.setTotal(total.intValue());
		pageVo.setRows(list);
		pageVo.setPage(page);
		return pageVo;

	}

    
}
